package controller

import (
	"com.wen/logic"
	"fmt"
	"github.com/gin-gonic/gin"
	"net/http"
	"strconv"
)

func IndexHandle(ctx *gin.Context) {
	articleRecordList, err := logic.GetArticleRecordList(0, 15)
	if err != nil {
		fmt.Printf("get article failed, err:%v\n", err)

		ctx.HTML(http.StatusInternalServerError, "views/500.html", nil)
		return
	}
	allCategoryList, err := logic.GetAllCategoryList()
	if err != nil {
		fmt.Printf("get category list failed, err:%v\n", err)
	}
	var data map[string]interface{} = make(map[string]interface{}, 10)
	data["article_list"] = articleRecordList
	data["category_list"] = allCategoryList
	ctx.HTML(http.StatusOK, "views/index.html", data)
	//ctx.JSON(http.StatusOK, gin.H{ "ldata":data})
}

func ArticleDetail(ctx *gin.Context) {
	articleIdStr := ctx.Query("article_id")
	articleId, err := strconv.ParseInt(articleIdStr, 10, 64)
	if err != nil {
		fmt.Printf("get article detail failed,article_id:%d err:%v\n", articleId, err)
		ctx.HTML(http.StatusInternalServerError, "views/500.html", nil)
		return
	}
	//fmt.Printf("获取文档 failed,article_id:%d err:%v\n", articleId, err)
	articleDetail, err := logic.GetArticleDetail(articleId)
	if err != nil {
		fmt.Printf("获取文档 failed,article_id:%d err:%v\n", articleId, err)
		ctx.HTML(http.StatusInternalServerError, "views/500.html", nil)
		return
	}
	fmt.Printf("article detail:%#v\n", articleDetail)
	allCategoryList, err := logic.GetAllCategoryList()

	if err != nil {
		fmt.Printf("get all category failed, err:%v\n", err)
	}

	relativeArticle, err := logic.GetRelativeAricleList(articleId)
	if err != nil {
		fmt.Printf("get relative article failed, err:%v\n", err)
	}

	prevArticle, nextArticle, err := logic.GetPrevAndNextArticleInfo(articleId)
	if err != nil {
		fmt.Printf("get prev or next article failed, err:%v\n", err)
	}

	commentList, err := logic.GetCommentList(articleId)

	if err != nil {
		fmt.Printf("get comment list failed, err:%v\n", err)
	}

	fmt.Printf("relative article size:%d article_id:%d\n", len(relativeArticle), articleId)

	var m map[string]interface{} = make(map[string]interface{}, 10)

	m["detail"] = articleDetail
	m["relative_article"] = relativeArticle
	m["prev"] = prevArticle
	m["next"] = nextArticle
	m["category"] = allCategoryList
	m["article_id"] = articleId
	m["comment_list"] = commentList

	ctx.HTML(http.StatusOK, "views/detail.html", m)
}

func NewArticle(ctx *gin.Context) {
	categoryList, err := logic.GetAllCategoryList()
	if err != nil {
		fmt.Printf("get article failed, err:%v\n", err)
		ctx.HTML(http.StatusInternalServerError, "views/500.html", nil)
		return
	}
	ctx.HTML(http.StatusOK, "views/post_article.html", categoryList)
}

//文章提交接口
func ArticleSubmit(ctx *gin.Context) {
	content := ctx.PostForm("content")
	author := ctx.PostForm("author")
	categoryIdStr := ctx.PostForm("category_id")
	title := ctx.PostForm("title")

	//转为十进制 64位
	categoryId, err := strconv.ParseInt(categoryIdStr, 10, 64)

	if err != nil {
		ctx.HTML(http.StatusInternalServerError, "views/500.html", nil)
		return
	}
	err = logic.InsertArticle(content, author, title, categoryId)
	if err != nil {
		ctx.HTML(http.StatusInternalServerError, "views/500.html", nil)
		return
	}
	ctx.Redirect(http.StatusMovedPermanently, "/")

}
